package Controller

import (
	"github.com/gin-gonic/gin"
	"learningProject/vueGoProject/common"
	"learningProject/vueGoProject/model"
	"learningProject/vueGoProject/response"
	"learningProject/vueGoProject/vo"
	"strconv"
)

type IpostController interface {
	RestController
	PageList(c *gin.Context) // 分页方法
}

type PostController struct {
}

func NewPostController() IpostController {
	common.DB.AutoMigrate(model.Post{})
	return PostController{}
}

func (p PostController) Create(c *gin.Context) {
	var requestPost vo.CreatePostRequest
	// 数据验证
	if err := c.ShouldBind(&requestPost); err != nil {
		response.Fail(c, nil, "数据验证错误！")
		return
	}

	// 获取登录用户user
	user, _ := c.Get("user") // 从上下文获取

	// 创建书籍信息post
	post := model.Post{
		// uuid已经设置为自动填写
		UserId:     user.(model.User).ID,
		CategoryId: requestPost.CategoryId,
		Title:      requestPost.Title,
		HeadImg:    requestPost.HeadImg,
		Content:    requestPost.Content,
	}

	// 插入数据
	if err := common.DB.Create(&post).Error; err != nil {
		panic(err)
		//response.Fail(c,nil,"创建失败！")
		return
	}
	response.Success(c, gin.H{"post": post}, "创建成功")
}

func (p PostController) Update(c *gin.Context) {
	var requestPost vo.CreatePostRequest
	// 数据验证
	if err := c.ShouldBind(&requestPost); err != nil {
		response.Fail(c, nil, "数据验证错误！")
		return
	}

	// 获取path中的id
	postId := c.Params.ByName("id")
	var post model.Post
	if common.DB.Where("id=?", postId).First(&post).RecordNotFound() {
		response.Fail(c, nil, "文章不存在！")
		return
	}

	// 判断当前用户是否为文章作者
	// 获取登录用户user
	user, _ := c.Get("user") // 从上下文获取
	userId := user.(model.User).ID
	if userId != post.UserId {
		response.Fail(c, nil, "文章不属于你！")
		return
	}

	// 更新文章
	if err := common.DB.Model(&post).Update(requestPost).Error; err != nil {
		response.Fail(c, nil, "文章更新失败！")
		return
	}
	response.Success(c, gin.H{"post": post}, "更新成功")
}

func (p PostController) Show(c *gin.Context) {
	// 获取path中的id
	postId := c.Params.ByName("id")
	var post model.Post
	// preload是关联查询，能把categories表的东西查出来加入posts表
	if common.DB.Preload("Category").Where("id=?", postId).First(&post).RecordNotFound() {
		response.Fail(c, nil, "文章不存在！")
		return
	}

	response.Success(c, gin.H{"post": post}, "成功")
}

func (p PostController) Delete(c *gin.Context) {
	// 获取path中的id
	postId := c.Params.ByName("id")
	var post model.Post
	if common.DB.Where("id=?", postId).First(&post).RecordNotFound() {
		response.Fail(c, nil, "文章不存在！")
		return
	}

	user, _ := c.Get("user") // 从上下文获取
	userId := user.(model.User).ID
	if userId != post.UserId {
		response.Fail(c, nil, "文章不属于你！")
		return
	}

	// 删除文章
	if err := common.DB.Delete(&post).Error; err != nil {
		response.Fail(c, nil, "删除失败！")
		return
	}
	response.Success(c, nil, "删除成功")
}

func (p PostController) PageList(c *gin.Context) {
	// 获取分页参数
	pageNum, _ := strconv.Atoi(c.DefaultQuery("pageNum", "1"))    // 分多少页
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "20")) // 每页多少

	// 分页
	var posts []model.Post
	common.DB.Order("created_at desc").Offset((pageNum - 1) * pageSize).Limit(pageSize).Find(&posts)

	// 前端分页需要知道记录总条数
	var total int
	common.DB.Model(model.Post{}).Count(&total)

	response.Success(c, gin.H{"posts": posts, "total": total}, "分页成功")

}
